package api

import (
	"baidu_api/basic/globar"
	"baidu_api/handler/request"
	"baidu_rpc/kitex_gen/baidu_rpc/user"
	"context"
	"fmt"
	"io/ioutil"
	"net/url"
	"time"

	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/common/utils"

	"net/http"
)

func Register(ctx context.Context, c *app.RequestContext) {
	var req request.UserReq
	if err := c.Bind(&req); err != nil {
		c.JSON(http.StatusInternalServerError, utils.H{
			"code":    http.StatusInternalServerError,
			"message": err.Error(),
			"data":    false,
		})
		return
	}
	register, err := globar.UserClient.GetRegister(ctx, &user.GetRegisterReq{
		Mobile:   req.Mobile,
		Password: req.Password,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, utils.H{
			"code":    http.StatusBadRequest,
			"message": err.Error(),
			"data":    false,
		})
		return
	}
	c.JSON(http.StatusOK, utils.H{
		"code":    http.StatusOK,
		"message": "注册成功",
		"data":    register,
	})
	return
}

func Login(ctx context.Context, c *app.RequestContext) {
	var req request.UserReq
	if err := c.Bind(&req); err != nil {
		c.JSON(http.StatusInternalServerError, utils.H{
			"code":    http.StatusInternalServerError,
			"message": err.Error(),
			"data":    false,
		})
		return
	}
	login, err := globar.UserClient.GetLogin(ctx, &user.GetLoginReq{
		Mobile:   req.Mobile,
		Password: req.Password,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, utils.H{
			"code":    http.StatusBadRequest,
			"message": err.Error(),
			"data":    false,
		})
		return
	}
	c.JSON(http.StatusOK, utils.H{
		"code":    http.StatusOK,
		"message": "登录成功",
		"data":    login,
	})
	return
}

func Locate(ctx context.Context, c *app.RequestContext) {
	ak := "bsSryvTCCuT2Xsetr5UIBMXzOxNL6Ih9"

	// 服务地址
	host := "https://api.map.baidu.com"

	// 接口地址
	uri := "/place/v2/search"

	// 设置请求参数
	params := url.Values{
		"query":  []string{"ATM机"},
		"tag":    []string{"银行"},
		"region": []string{"上海"},
		"output": []string{"json"},
		"ak":     []string{ak},
	}

	// 发起请求
	request, err := url.Parse(host + uri + "?" + params.Encode())
	if nil != err {
		fmt.Printf("host error: %v", err)
		return
	}

	resp, err1 := http.Get(request.String())
	fmt.Printf("url: %s\n", request.String())
	defer resp.Body.Close()
	if err1 != nil {
		fmt.Printf("request error: %v", err1)
		return
	}
	body, err2 := ioutil.ReadAll(resp.Body)
	if err2 != nil {
		fmt.Printf("response error: %v", err2)
	}
	c.JSON(http.StatusOK, utils.H{
		"code":    http.StatusOK,
		"message": "查询成功",
		"data":    string(body),
	})

	fmt.Println(string(body))

	return
}

func Address(ctx context.Context, c *app.RequestContext) {
	ak := "bsSryvTCCuT2Xsetr5UIBMXzOxNL6Ih9"

	// 服务地址
	host := "https://api.map.baidu.com"

	// 接口地址
	uri := "/location/ip"
	key := fmt.Sprintf("Ip:%s", "180.171.140.123")
	globar.Rdb.Set(ctx, key, ak, time.Minute*5)
	globar.Rdb.Incr(ctx, key)
	globar.Rdb.Expire(ctx, key, time.Minute*5)
	result, err := globar.Rdb.Get(ctx, key).Result()
	if result != "" {
		c.JSON(http.StatusBadRequest, utils.H{
			"code":    http.StatusBadRequest,
			"message": "情不要尝试刷新定位",
			"data":    false,
		})
		return
	}

	// 设置请求参数
	params := url.Values{
		"ip":   []string{"180.171.140.123"},
		"coor": []string{"bd09ll"},
		"ak":   []string{ak},
	}

	// 发起请求
	request, err := url.Parse(host + uri + "?" + params.Encode())
	if nil != err {
		fmt.Printf("host error: %v", err)
		return
	}

	resp, err1 := http.Get(request.String())
	fmt.Printf("url: %s\n", request.String())
	defer resp.Body.Close()
	if err1 != nil {
		fmt.Printf("request error: %v", err1)
		return
	}
	body, err2 := ioutil.ReadAll(resp.Body)
	if err2 != nil {
		fmt.Printf("response error: %v", err2)
	}

	fmt.Println(string(body))
	c.JSON(http.StatusOK, utils.H{
		"code":    http.StatusOK,
		"message": "查询成功",
		"data":    string(body),
	})

	return
}

func Route(ctx context.Context, c *app.RequestContext) {
	ak := "bsSryvTCCuT2Xsetr5UIBMXzOxNL6Ih9"
	Url, err := c.Get(fmt.Sprintf("https://api.map.baidu.com/direction/v2/driving?origin=40.01116,116.339303&destination=39.936404,116.452562&ak=%s", ak))
	if err != true {
		c.JSON(http.StatusBadRequest, utils.H{
			"code":    http.StatusBadRequest,
			"message": "查询失败",
			"data":    false,
		})
		return
	}
	c.JSON(http.StatusOK, utils.H{
		"code":    http.StatusOK,
		"message": "查询成功",
		"data":    Url,
	})
}

func Count(ctx context.Context, c *app.RequestContext) {
	var req request.CountReq
	if err := c.Bind(&req); err != nil {
		c.JSON(http.StatusInternalServerError, utils.H{
			"code":    http.StatusInternalServerError,
			"message": err.Error(),
			"data":    false,
		})
		return
	}
	var count int64
	count += 3
	count = int64(req.Num)*3 + int64(req.Price)
	c.JSON(http.StatusOK, utils.H{
		"code":    http.StatusOK,
		"message": "你的费用是",
		"data":    count,
	})
	return

}
